package com.xiaoxu.intranetweb.mybatisplusSzappdb.controller.appointment;

import cn.dev33.satoken.stp.StpUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.business.appointment.AppointmentBu;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.dao.UserInfoDao;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.entity.SysRoleInfoFunctionWeb;
import com.xiaoxu.intranetweb.util.PermissionCheck;
import com.xiaoxu.intranetweb.util.SzResult;
import com.xiaoxu.intranetweb.util.WebFunctionUtil;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;
import java.util.Set;

@RestController
@RequestMapping(value = "/appointment")
public class AppointmentController {
    @Autowired
    private AppointmentBu appointmentBu;
    @Autowired
    private UserInfoDao userInfoDao;
    @Autowired
    private WebFunctionUtil webFunctionUtil;

    @PostMapping("/getAppointment")
    public SzResult getAppointment(@RequestBody JSONObject jsonObject, HttpServletRequest request) throws Exception {
        //String company, String product, String status, String token,String plantId
        String company = jsonObject.getString("company");
        String product = jsonObject.getString("product");
        String status = jsonObject.getString("status");
        String startTime = jsonObject.getString("startTime");
        String endTime = jsonObject.getString("endTime");
        String token = request.getHeader("Authorization");
        String plantId = request.getHeader("Plant-Id");

//        if (!userInfoDao.getUserPermissionWeb(StpUtil.getLoginIdByToken(token).toString()).containsKey("getAppointment")) {
//            return SzResult.error("您没有查询预约信息的权限");
//        }
        return appointmentBu.getAppointment(company, product, status, startTime, endTime, token, plantId);
    }


    //    新增预约信息
    @PostMapping("/addAppointment")
    @PermissionCheck("addAppointment")
    public SzResult addAppointment(@RequestBody JSONObject jsonObject, HttpServletRequest request) throws Exception {
        String token = request.getHeader("Authorization");
        String plantId = request.getHeader("Plant-Id");
        String cpyId = jsonObject.getString("cpyId");
        String productId = jsonObject.getString("productId");
        String appointCount = jsonObject.getString("appointCount");
        String type = jsonObject.getString("type");

        return appointmentBu.addAppointment(cpyId, productId, appointCount, type, token, plantId);
    }

    //审批预约信息
    @PostMapping("/approveAppointment")
    @PermissionCheck("approveAppointment")
    public SzResult approveAppointment(@RequestBody JSONObject jsonObject, HttpServletRequest request) throws Exception {
        String token = request.getHeader("Authorization");
        String plantId = request.getHeader("Plant-Id");
        String id = jsonObject.getString("id");
        String status = jsonObject.getString("status");
        String winCount = jsonObject.getString("winCount");
        return appointmentBu.approveAppointment(id, status, winCount, token, plantId);
    }

    //删除预约信息
    @GetMapping("/deleteAppointment")
    @PermissionCheck("deleteAppointment")
    public SzResult deleteAppointment(@RequestParam String id, HttpServletRequest request) throws Exception {
        return appointmentBu.deleteAppointment(id);
    }

    //修改预约信息
    @PostMapping("/updateAppointment")
    @PermissionCheck("updateAppointment")
    public SzResult updateAppointment(@RequestBody JSONObject jsonObject, HttpServletRequest request) throws Exception {
        String token = request.getHeader("Authorization");
        String plantId = request.getHeader("Plant-Id");
        String id = jsonObject.getString("id");
        String appointCount = jsonObject.getString("appointCount");
        String type = jsonObject.getString("type");
        return appointmentBu.updateAppointment(id, appointCount, token, plantId);
    }


}
